﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class DánhachKhachHangKhongMuaSPTrongThang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienThi();
            }
        }
        public void LoadHienThi()
        {
            DateTime dt = DateTime.Now;
            int thang = int.Parse(dt.Month.ToString());
            int nam = int.Parse(dt.Year.ToString());
            Dlthang.SelectedValue = thang.ToString();
            Dlnam.SelectedValue = nam.ToString();
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            KHACHHANGRepository repositoryKh = new KHACHHANGRepository(new EFRepository<KHACHHANG>(), iunitfwork);
            CHITIETHOADONXUATRepository repository = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
            List<string> LstMaHdx = repository.All().Where(p => p.HOADONXUAT.NGAYXUAT.Value.Month == thang && p.HOADONXUAT.NGAYXUAT.Value.Year == nam).Select(p => p.HOADONXUAT.MAKH).ToList();
            List<string> LstKhachhangKomuahang = repositoryKh.All().Where(p => !LstMaHdx.Contains(p.MAKH)).Select(p => p.MAKH).ToList();
            var LstKhkoMuaHang = repositoryKh.All().Where(p =>LstKhachhangKomuahang.Contains(p.MAKH)).ToList();
            Grdanhsang.DataSource = LstKhkoMuaHang;
            Grdanhsang.DataBind();
        }
        public void Xem()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            int thang=int.Parse(Dlthang.SelectedValue);
            int nam=int.Parse(Dlnam.SelectedValue);
            KHACHHANGRepository repositoryKh = new KHACHHANGRepository(new EFRepository<KHACHHANG>(), iunitfwork);
            CHITIETHOADONXUATRepository repository = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
            List<string> LstMaHdx = repository.All().Where(p => p.HOADONXUAT.NGAYXUAT.Value.Month == thang && p.HOADONXUAT.NGAYXUAT.Value.Year == nam).Select(p => p.HOADONXUAT.MAKH).ToList();
            List<string> LstKhachhangKomuahang = repository.All().Where(p => !LstMaHdx.Contains(p.HOADONXUAT.MAKH)).Select(p => p.HOADONXUAT.MAKH).ToList();
            var LstKhkoMuaHang = repositoryKh.All().Where(p =>!LstKhachhangKomuahang.Contains(p.MAKH)).ToList();
            Grdanhsang.DataSource = LstKhkoMuaHang;
            Grdanhsang.DataBind();
           

        }
        protected void Grdanhsang_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
           
        }

        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            Xem();
        }

        protected void Grdanhsang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadHienThi();
            Grdanhsang.PageIndex = e.NewPageIndex;
            Grdanhsang.DataBind();
        }
        public void TimKiem()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            KHACHHANGRepository repository = new KHACHHANGRepository(new EFRepository<KHACHHANG>(), iunitfwork);
            IQueryable<KHACHHANG> objkhachhang = null;
            if (!string.IsNullOrEmpty(txtmakh.Value))
            {
                objkhachhang = repository.All().Where(p => p.MAKH.Contains(txtmakh.Value));
                Grdanhsang.DataSource = objkhachhang;
                Grdanhsang.DataBind();
            }
            if (!string.IsNullOrEmpty(txttenkh.Value))
            {
                objkhachhang = repository.All().Where(p => p.TENKH.Contains(txttenkh.Value));
                Grdanhsang.DataSource = objkhachhang;
                Grdanhsang.DataBind();
            }

            if (!string.IsNullOrEmpty(txtmakh.Value) && !string.IsNullOrEmpty(txttenkh.Value))
            {
                objkhachhang = repository.All().Where(p => p.MAKH.Contains(txtmakh.Value) && p.TENKH.Contains(txttenkh.Value));
                Grdanhsang.DataSource = objkhachhang;
                Grdanhsang.DataBind();
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            TimKiem();

        }
    }
}